
<?php

/*
  Document   : admin
  Created on : 12-mei-2011, 12:05:08
  Author     : Shahram abdi
  Description:
 */

// load configuration file
require_once('config/config.php');
require_once('config/error_handler.php');

// Connenct class that contains server-side chat functionality
class Connenct {

    // database handler
    private $mMysqli;

    // constructor opens database connection
    function __construct() {
        // connect to the database
        $this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
    }

    // destructor closes database connection
    public function __destruct() {
        $this->mMysqli->close();
    }

    //The postMessages method inserts a profile into the database
    public function postTopic($topicTitel, $topicText, $relatieNummer) {

        // escape the variable data for safely adding them to the database
        $topicTitel = $this->mMysqli->real_escape_string($topicTitel);
        $topicText = $this->mMysqli->real_escape_string($topicText);

        // build the SQL query that adds a new message to the server
        $query = 'INSERT INTO forum(datum, topicTitel, topicText, relatieNummer) ' .
                'VALUES (NOW(), "' . $topicTitel . '","' . $topicText . '","' . $relatieNummer . '")';
        // execute the SQL query
        $result = $this->mMysqli->query($query);
        echo 'Topic is toegevoegd.';
    }

    //The postMessages method inserts a profile into the database
    public function postReactie($reactieTitel, $reactieText, $relatieNummer) {

        // escape the variable data for safely adding them to the database
        $reactieTitel = $this->mMysqli->real_escape_string($reactieTitel);
        $reactieText = $this->mMysqli->real_escape_string($reactieText);

        // build the SQL query that adds a new message to the server
        $query = 'INSERT INTO reacties(datum, reactieTitel, reactieText, relatieNummer) ' .
                'VALUES (NOW(), "' . $reactieTitel . '","' . $reactieText . '","' . $relatieNummer . '")';
        // execute the SQL query
        $result = $this->mMysqli->query($query);
        echo 'reactie is toegevoegd.';
    }

    public function retrieveNewNews() {
        $query = "SELECT * FROM nieuws ORDER BY id_nieuws DESC LIMIT 1 ";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['titel'] = $row['titel'];
                $message['tekst'] = $row['tekst'];
                $message['datum'] = $row['datum'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function retrievelogo() {
        $query = "SELECT instellingnaam, logo FROM werkgeverprofiel";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['instellingnaam'] = $row['instellingnaam'];
                $message['logo'] = $row['logo'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function retrievestage() {
        $query = "SELECT stageplaats, stagetitel FROM stages";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['stageplaats'] = $row['stageplaats'];
                $message['stagetitel'] = $row['stagetitel'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function retrieveVacatures() {
        $query = "SELECT vacaturetitel, vacaturplaats FROM vacatures";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['vacaturetitel'] = $row['vacaturetitel'];
                $message['vacaturplaats'] = $row['vacaturplaats'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function retrieveAllNews() {
        $query = "SELECT * FROM nieuws ORDER BY id_nieuws DESC ";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['id_nieuws'] = $row['id_nieuws'];
                $message['titel'] = $row['titel'];
                $message['tekst'] = $row['tekst'];
                $message['link'] = $row['link'];
                $message['datum'] = $row['datum'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function retrieveAllTopics() {
        $query = "SELECT * FROM forum ORDER BY topicID DESC ";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['topicID'] = $row['topicID'];
                $message['topicTitel'] = $row['topicTitel'];
                $message['topicText'] = $row['topicText'];
                $message['datum'] = $row['datum'];
                $message['relatieNummer'] = $row['relatieNummer'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }
}
?>
